﻿using System;
using System.Net;
using System.Net.Mail;

/// <summary>
/// Class contains miscellaneous functionality 
/// </summary>
public static class Utilities
{
    static Utilities()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    // Generic method for sending emails
    public static void SendMail(string from, string to, string subject,
    string body)
    {
        // Configure mail client
        SmtpClient mailClient = new SmtpClient(AcheiCampinasConfiguration.MailServer);
        // Set credentials (for SMTP servers that require authentication)
        mailClient.Credentials = new NetworkCredential(AcheiCampinasConfiguration.MailUsername, AcheiCampinasConfiguration.MailPassword);
        // Create the mail message
        MailMessage mailMessage = new MailMessage(from, to, subject, body);
        // Send mail
        mailClient.Send(mailMessage);
    }


    // Generic method for sending emails
    public static void EnviarMail(string from, string nome, string to, string subject, string body)
    {
        //o e-mail do remetente
        MailMessage mail = new MailMessage();
        mail.To.Add(to);
        mail.From = new MailAddress(from, nome, System.Text.Encoding.UTF8);
        mail.Subject = subject;
        mail.SubjectEncoding = System.Text.Encoding.UTF8;
        mail.Body = body;
        mail.BodyEncoding = System.Text.Encoding.UTF8;
        mail.IsBodyHtml = true;
        //Prioridade do E-Mail
        mail.Priority = MailPriority.High;

        SmtpClient client = new SmtpClient();
        //Adicionando as credenciais do seu e-mail e senha:
        client.Credentials = new System.Net.NetworkCredential(AcheiCampinasConfiguration.MailTo, AcheiCampinasConfiguration.MailPassword);
        // Esta porta é a utilizada pelo Gmail para envio
        client.Port = 25;

        //Definindo o provedor que irá disparar o e-mail
        client.Host = "mail.uniquenet.com.br";

        //Gmail trabalha com Server Secured Layer
        client.EnableSsl = false;

        client.Send(mail);
    }



    // Send error log mail
    public static void LogError(Exception ex)
    {
        // get the current date and time
        string dateTime = DateTime.Now.ToLongDateString() + ", at "
                      + DateTime.Now.ToShortTimeString();
        // stores the error message
        string errorMessage = "Exception generated on " + dateTime;
        // obtain the page that generated the error
        System.Web.HttpContext context = System.Web.HttpContext.Current;
        errorMessage += "\n\n Page location: " + context.Request.RawUrl;
        // build the error message
        errorMessage += "\n\n Message: " + ex.Message;
        errorMessage += "\n\n Source: " + ex.Source;
        errorMessage += "\n\n Method: " + ex.TargetSite;
        errorMessage += "\n\n Stack Trace: \n\n" + ex.StackTrace;
        // send error email in case the option is activated in Web.Config
        if (AcheiCampinasConfiguration.EnableErrorLogEmail)
        {
            string from = AcheiCampinasConfiguration.MailTo;
            string to = AcheiCampinasConfiguration.ErrorLogEmail;
            string subject = "BalloonShop Error Report";
            string body = errorMessage;
            SendMail(from, to, subject, body);
        }
    }
}
